package com.example.administrator.myapplication;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.myapplication.model.User;
import com.example.administrator.myapplication.service.DBManager;
import com.example.administrator.myapplication.service.LoginJudge;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link RegisterFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class RegisterFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static final String TAG = "RegisterFragment";
    private DBManager dbManager;
    private Button ignore_register_btn;
    private Button register_btn;
    private Button log_in_btn;
    private EditText account_et;
    private EditText password_et;
    private User user;
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;


    public RegisterFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment RegisterFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static RegisterFragment newInstance(String param1, String param2) {
        RegisterFragment fragment = new RegisterFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onStart() {
        super.onStart();

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        user = new User();
        dbManager = new DBManager(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_register, container, false);
        account_et = ((EditText) view.findViewById(R.id.account_et));
        password_et = ((EditText) view.findViewById(R.id.password_et));





        ignore_register_btn = ((Button) view.findViewById(R.id.ignore_register_btn));
        ignore_register_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(TAG, "忽略注册，直接跳转至。。。");
                Intent intent = new Intent(getActivity(), MainActivity.class);
                startActivity(intent);
                getActivity().finish();
            }
        });

        log_in_btn = ((Button) view.findViewById(R.id.log_in_btn));
        log_in_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                user.setUsername(account_et.getText().toString());
                user.setPassword(password_et.getText().toString());
                Log.i(TAG, "onClick: 即将传出的User对象："+user.getUsername());
                if (dbManager.query(user)) {
                    //将账户的状态设为已登录
                    new LoginJudge(getActivity()).setAccountLandingStatus(user.getUsername());

                    Intent intent = new Intent(getActivity(), MainActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("userObject",user);
                    intent.putExtra("account",bundle);
                    startActivity(intent);
                    getActivity().finish();
                }else {
                    Toast.makeText(getActivity(),"账户/密码错误，或账户未注册！",Toast.LENGTH_SHORT).show();
                }
            }
        });

        register_btn = ((Button) view.findViewById(R.id.register_btn));
        register_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //每一次触发点击事件时都需要重新获取值
                user.setUsername(account_et.getText().toString());
                user.setPassword(password_et.getText().toString());
                Log.i(TAG, "onClick: 即将传出的User对象："+user.getUsername());
                if(dbManager.add(user)) {
                    Toast.makeText(getActivity(), "注册成功！", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(getActivity(), "注册失败！", Toast.LENGTH_SHORT).show();
                }


            }
        });


        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        getActivity().setTitle("注册/登陆");
    }


}
